package org.example;

public class Max {
    public int maxSubArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int maxSum = nums[0];
        int currentSum = nums[0];

        for (int i = 1; i < nums.length; i++) {
            currentSum = Math.max(nums[i], currentSum + nums[i]);
            maxSum = Math.max(maxSum, currentSum);
        }

        return maxSum;
    }

    public static void main(String[] args) {
        Max max = new Max();
        int[] nums1 = {-4, 3, 5, 4, -1};
        System.out.println(max.maxSubArray(nums1));

        int[] nums2 = {-9, 2, -2, 8, 7};
        System.out.println(max.maxSubArray(nums2));

        int[] nums3 = {5, 5, 5, -4, -1};
        System.out.println(max.maxSubArray(nums3));
    }
}

